package logic.framework_logic;

import java.util.LinkedList;
import java.util.List;

import javaRMI.RMIClient;
import logic.utility_logic.CourseProduceLogicsvFactory;
import logic.utility_logic.CourseQueryLogicsvFactory;
import logicsv.courseQuery_logicsv.CourseQueryLogicsv;
import po.CourseModelPO;
import po.ModelPO;
import po.SubModelPO;
import resultmessage.FrameworkRM;
import vo.CourseVO;
import vo.ModelVO;
import vo.SubModelVO;
import datasv.datafactorysv.FrameworkDatasvFactory;
import datasv.framework_datasv.FrameworkDatasv;

public class Framework {
	private FrameworkDatasv frameworkData;
	private CourseQueryLogicsv courseQL;
	public Framework(RMIClient rmi) {
		super();
		frameworkData = new FrameworkDatasvFactory(rmi).create();
		new CourseProduceLogicsvFactory(rmi).create();
		courseQL = new CourseQueryLogicsvFactory(rmi).create();

	}
	
	public FrameworkRM deleteCouOfSubModel(String courseID,String subModelID){
		return frameworkData.deleteCouOfSubModel(courseID, subModelID);
	}
	public FrameworkRM addCouOfSubModel(String courseID,String subModelID) {
		return frameworkData.addCouOfSubModel(courseID, subModelID);
	}
	public CourseModelPO getSubModelOfCou(String courseID) {
		return frameworkData.getSubModelOfCou(courseID);
	}
	public FrameworkRM updateSubModelOfCou (String courseID, String submodelID) {
		return frameworkData.updateSubModelOfCou(courseID, submodelID);
	}
	
	
	
	public boolean canChooseCurriculum() {
		return frameworkData.canChooseCurriculum();
	}
	public boolean canChooseCrossMajor()  {
		return frameworkData.canChooseCrossMajor();
	}
	public boolean canChoosePublic()  {
		return frameworkData.canChoosePublic();
	}
	
	public boolean changeChooseCurriculum()  {
		return frameworkData.changeChooseCurriculum();
	}
	public boolean changeChooseCrossMajor()  {
		return frameworkData.changeChooseCrossMajor();
	}
	public boolean changeChoosePublic()  {
		return frameworkData.changeChoosePublic();
	}
	public FrameworkRM addModel(String collegeID, ModelPO m) {
		return frameworkData.addModel(collegeID, m);
	}

	public ModelVO getModel(String collegeID, ModelPO model){
		List<SubModelPO> subModelList = frameworkData.getAllSubModelOfModel(collegeID, model.getModelID());	
		LinkedList<SubModelVO> smvList = new LinkedList<SubModelVO>();
		if(subModelList != null){
			for(SubModelPO smp : subModelList){
				smvList.add(this.getSubModel(collegeID, model.getModelID(), smp));
			}
		}
		return new ModelVO(model,smvList);
	}
	public FrameworkRM deleteModel(String collegeID, String modelID) {
		return frameworkData.deleteModel(collegeID, modelID);
	}

	public FrameworkRM updateModel(String collegeID, ModelPO m) {
		return frameworkData.updateModel(collegeID, m);
	}

	public FrameworkRM addSubModel(String collegeID, String modelID,
			SubModelPO sm) {
		return frameworkData.addSubModel(collegeID, modelID, sm);
	}

	private SubModelVO getSubModel(String collegeID, String modelID,
			SubModelPO subModel){
		List<CourseVO> courses = courseQL.getAllCouOfSubModel(collegeID, modelID, subModel.getSubModelID());
		return new SubModelVO(subModel,courses);
	}
	public FrameworkRM deleteSubModel(String collegeID, String modelID,
			String subModelID) {
		// TODO Auto-generated method stub
		return frameworkData.deleteSubModel(collegeID, modelID, subModelID);
	}

	public FrameworkRM updateSubModel(String collegeID, String modelID,
			SubModelPO sm) {
		return frameworkData.updateSubModel(collegeID, modelID, sm);
	}
	private List<SubModelVO> getAllSubModelOfModel(String collegeID,String modelID){
		LinkedList<SubModelVO> list = new LinkedList<SubModelVO>();
		for(SubModelPO sub : frameworkData.getAllSubModelOfModel(collegeID, modelID)){
			list.add(this.getSubModel(collegeID, modelID, sub));
		}
		return list;
	}
	public List<ModelVO> getAllModelOfCollege(String collegeID){
		List<ModelPO> list = frameworkData.getCollegeModel(collegeID);
		LinkedList<ModelVO> modelList = new LinkedList<ModelVO>();
		for(ModelPO model: list){
			String modelID = model.getModelID();
			List<SubModelVO> smvList = this.getAllSubModelOfModel(collegeID, modelID);
			modelList.add(new ModelVO(model,smvList));
		}
		return modelList;
	}
	
	public String addModelReturnID(String collegeID, ModelPO model) {
		return frameworkData.addModelReturnID(collegeID, model);
	}
	public String addSubModelReturnID(String collegeID, String modelID,
			SubModelPO submodel) {
		// TODO Auto-generated method stub
		return frameworkData.addSubModelReturnID(collegeID, modelID, submodel);
	}
}
